require "grape"
require "grape-entity"

# ----------------------------------------------------
# 数据库连接，初始化全局变量
# ----------------------------------------------------
require_relative "config/env"

# ----------------------------------------------------
# DDD + MVC
# ----------------------------------------------------
require_relative "lib/auth_store"
require_relative "lib/auth_domain"
require_relative "lib/auth_entity"

module CLOAP
  class API < Grape::API
    version "v1", using: :path, vendor: "intermaker.cn"
    format :json
    prefix :api

    resource :jwt do
      desc "Authenticate"
      params do
        requires :uid,    type: String, desc: "user id in AD"
        requires :passwd, type: String, desc: "userPassword in AD"
      end
      post "/auth" do
        unless AuthDomain.auth(params[:uid], params[:passwd])
          status 401
          return present params, with: InvalidEntity, reason: "invalid"
        end

        status 200
        present params, with: AuthEntity
      end


      desc "Secret Code"
      get "/secret" do
        present Secret.current, with: SecretEntity
      end
    end
  end
end
